package android.cs2340UDT;

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.List;

public class Manager {
	
	private Map googleMaps;
	
	private Time clock;
	
	private ArrayList<ToDoList> toDoLists;
	private ArrayList<ItemLocation> itemLocations;
	
	private String currentListName;
	private ToDoList currentList;
	
	private String currentItemName;
	private Item currentItem;
	
	public Manager()
	{
		toDoLists = new ArrayList<ToDoList>();
		itemLocations = new ArrayList<ItemLocation>();
	}
	
	/**
	 * Adds a new ToDoList to the ArrayList of ToDoLists
	 * @param newList
	 */
	public void addNewList(ToDoList newList) {
		toDoLists.add(newList);
	}
	
	/**
	 * Creates a new list with the input name, then adds it
	 * @param name The name of the new list
	 */
	public void createNewList(String name){
		ToDoList list = new ToDoList(name);
		addNewList(list);
	}
	
	/**
	 * Returns an ArrayList of the names of all the ToDoLists the Manager has
	 * This method will need to be extended to return the Lists in various orders.
	 * @param order, an int which specifies what order the lists should be in. Will need to be enumerated later
	 * @return names, an ArrayList containing the names of all the Lists the Manager has
	 */
	public List<String> getListsNames(int order) {
		
		ArrayList<String> names = new ArrayList<String>();
		
		for(int i=0;i < toDoLists.size(); i++) {
			names.add(toDoLists.get(i).getName());
		}
		
		return names;
	}
	
	/**
	 * Returns the managers ArrayList of ToDoList objects
	 * @return manager's list of ToDoList objects
	 */
	public ArrayList<ToDoList> getToDoList(){
		return toDoLists;
	}
	
	public ArrayList<ItemLocation> getItemLocations(){
		return itemLocations;
	}
	
	public void addItemLocation(ItemLocation loc){
		itemLocations.add(loc);
	}
	
	/**
	 * Sets the ToDoList for the application to currently use. The name of the list is used to locate the ToDoList object.
	 * @param listName The name of the ToDoList object to set the current list to
	 */
	public void setCurrentList(String listName) {
		currentListName = listName;
		boolean found = false;
		// Check through all the ToDoLists
		for(ToDoList list : toDoLists) {
			// Check if any of the ToDoList object's names matches the input name
			if(list.getName().equals(currentListName) && !found) {
				// if the ToDoList is found, set the current list
				currentList = list;
				found = true;
			}
		}
		// If a ToDoList with the input name was not found, set the current list to a list containing the items of all the lists
		if(!found) {
			currentList = new ToDoList("Everything");
			for(ToDoList list : toDoLists) {
				for(Item item : list.getList()) {
					currentList.createItem(item.getDescription(), item.getLocation(), item.getDueDate(), item.getComment());
				}
			}
		}
	}
	
	/**
	 * Returns Manager's current list
	 * @return The current ToDoList in use
	 */
	public ToDoList getCurrentItemList() {
		
		return currentList;
	}
	

	/**
	 * Adds a tast (item) to the current ToDoList in use
	 * @param newItem The item to add to the current list
	 */
	public void addItemToCurrentList(Item newItem) {
		currentList.addItem(newItem);
		
	}
	
	public void updateCurrentItem(Item item) {
		currentItem = item;
	}
	
	/**
	 * Sets the current task (item) for use
	 * @param itemName The current item to be set for use
	 */
	public void setCurrentItem(String itemName) {
		boolean found = false;
		currentItemName = itemName;
		for(Item item : currentList.getList()) {
			if(item.getDescription().equals(itemName) && !found) {
				currentItem = item;
				found = true;
			}
		}
	}
	
	/**
	 * Returns the current item in use
	 * @return the current item in use
	 */
	public Item getCurrentItem() {
		return currentItem;
	}
	
	
	/**
	 * Regex checks if the date holds 8 digits
	 * @param date the data
	 * @return true, if successful
	 */
	public static boolean testDate(String date){
		Pattern p = Pattern.compile("[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{5}");
		return p.matcher(date).matches();
	}
	
	/**
	 * regex checks to see it name is corrent
	 * @param name the name
	 * @return true, if successfull
	 */
	public static boolean testName(String name){
		Pattern p = Pattern.compile("[A-Z]([a-z]|-|[A-Z])* [A-Z]([a-z]|-|[A-Z])*");
		return p.matcher(name).matches();
	}

	
}
